<?php
/**
* wapinfo.php
*
* Create on 2015-6-8
* Create by TT
*/
namespace haibao\cms\data;


class WapInfo extends \haibao\cms\data\BaseMysql {
	public function __construct() {
		parent::__construct ( '\haibao\cms\model\data\WapInfo' );
	}
	
	public function updateWapInfo($id,$arrSet){
		$this->updateById($id, $arrSet);
	}
	public function publishAll($ids){
		$str = preg_replace('/(\d+)/', '%s', implode(',', $ids));
		$today = date('Y-m-d H:i:s',time());
		//$sql1 = "update WapInfo set Published=2 where Id in (".$str.") and PublishTime > '".$today."' ";
// 		$sql2 = "update WapInfo set Published=3 where Id in (".$str.") and PublishTime < '".$today."' ";
		$sql = "update WapInfo set Published=3 where Id in (".$str.")";
		$this->query($sql,$ids);
		return true;
	}
	
	/**
	 * 得预发布文章与图集ID
	 * @return multitype:
	 */
	public function getPublishInfoIds(){
		$infoIds = array();
		$today = date('Y-m-d H:i:s',time());
		$sql="select Id,InfoId,InfoType from WapInfo where Published = ".\haibao\cms\model\data\WapInfo::PUBLISHED_PRE." and PublishTime<='".$today."' ";
		$result = $this->query($sql,array());
		while($result->fetch_assoc()){
			$infoIds[] = array('Id'=>$result->getData('Id'),'InfoId'=>$result->getData('InfoId'),'InfoType'=>$result->getData('InfoType'));
		}
		return $infoIds;
	}
	
	public function getPublishIngInfoIds(){
		$infoIds = array();
		$sql="select Id,InfoId,InfoType from WapInfo where Published = ".\haibao\cms\model\data\WapInfo::PUBLISHED_ING."";
		$result = $this->query($sql,array());
		while($result->fetch_assoc()){
			$infoIds[] = array('Id'=>$result->getData('Id'),'InfoId'=>$result->getData('InfoId'),'InfoType'=>$result->getData('InfoType'));
		}
		return $infoIds;
	}
	
	/**
	 * 修改文章、图集已发布状态
	 * @param unknown_type $infoId
	 * @param unknown_type $published
	 */
	public function updateInfoPublish($infoId, $published = \haibao\cms\model\data\WapInfo::PUBLISHED_YES) {
		$sql = "";
		$ids = array();
		if (is_int ( $infoId )) {
			$sql = "update WapInfo set Published=%s where  Id=%s";
			$arrValues = array (
					$published,
					$infoId
			);
			$this->query ( $sql, $arrValues );
			$ids = array($infoId);
		} elseif (is_array ( $infoId )) {
			$Ids = join(',',$infoId);
			$sql = "update WapInfo set Published=".$published." where Id in (".$Ids.")  ";
			$this->query ( $sql );
			$ids = $infoId;
		}
		
		if ($published == \haibao\cms\model\data\WapInfo::PUBLISHED_YES){
			$sql = 'update Article a inner join WapInfo w on w.InfoType = '.\haibao\cms\model\data\WapInfo::INFO_TYPE_ARTICLE.' and w.InfoId=a.Id set IndexTime = "'.date('Y-m-d H:i:s').'" where w.Id in('.implode(',', $ids).')';
			$this->query($sql);
			$sql = 'update ImageCollective a inner join WapInfo w on w.InfoType = '.\haibao\cms\model\data\WapInfo::INFO_TYPE_IMAGE_COLLECTIVE.' and w.InfoId=a.Id set IndexTime = "'.date('Y-m-d H:i:s').'" where w.Id in('.implode(',', $ids).')';
			$this->query($sql);
		}
	}
	
}